home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_337 / cmanual / sprites.lzh / Sprites / Example2.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  11KB  |  393 lines

  1. /* Example2                                                            */
  2. /* This program shows how to declare and initialize some sprite data   */
  3. /* and a SimpleSprite structure. It also shows how to reserve a sprite */
  4. /* (sprite 2), and how to move it around. The user moves the sprite by */
  5. /* pressing the arrow keys. In this example we animate the sprite (6   */
  6. /* frames taken from Miniblast).                                       */
  7.  
  8.  
  9.  
  10. #include <intuition/intuition.h>
  11. /* Include this file since you are using sprites: */
  12. #include <graphics/sprite.h>
  13.  
  14.  
  15.  
  16. /* Declare the functions we are going to use: */
  17. void main();
  18. void free_memory();
  19.  
  20.  
  21.  
  22. struct IntuitionBase *IntuitionBase = NULL;
  23. /* We need to open the Graphics library since we are using sprites: */
  24. struct GfxBase *GfxBase = NULL;
  25.  
  26.  
  27.  
  28. /* Declare a pointer to a Window structure: */ 
  29. struct Window *my_window = NULL;
  30.  
  31. /* Declare and initialize your NewWindow structure: */
  32. struct NewWindow my_new_window=
  33. {
  34.   50,            /* LeftEdge    x position of the window. */
  35.   25,            /* TopEdge     y positio of the window. */
  36.   320,           /* Width       320 pixels wide. */
  37.   100,           /* Height      100 lines high. */
  38.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  39.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  40.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  41.   RAWKEY,        /*             user has selected the Close window gad, */
  42.                  /*             or if the user has pressed a key. */
  43.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  44.   WINDOWCLOSE|   /*             Close Gadget. */
  45.   WINDOWDRAG|    /*             Drag gadget. */
  46.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  47.   WINDOWSIZING|  /*             Sizing Gadget. */
  48.   ACTIVATE,      /*             The window should be Active when opened. */
  49.   NULL,          /* FirstGadget No Custom gadgets. */
  50.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  51.   "MICROBLAST",  /* Title       Title of the window. */
  52.   NULL,          /* Screen      Connected to the Workbench Screen. */
  53.   NULL,          /* BitMap      No Custom BitMap. */
  54.   80,            /* MinWidth    We will not allow the window to become */
  55.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  56.   300,           /* MaxWidth    than 300 x 200. */
  57.   200,           /* MaxHeight */
  58.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  59. };
  60.  
  61.  
  62.  
  63. /*********************************************************************/
  64. /* Extra information:                                                */
  65. /* When we declare the window pointer, the intuition library pointer */
  66. /* etc, we initialize them to point to NULL:                         */
  67. /* struct Window *my_window = NULL;                                  */
  68. /* Since we then know that all of the pointers will point to NULL    */
  69. /* when we start, we can check if they still point to NULL when we   */
  70. /* quit. If they do not point to NULL anymore, we close that window, */
  71. /* library etc.                                                      */
  72. /*********************************************************************/
  73.  
  74.  
  75.  
  76. /********************************************************/
  77. /* 1. Declare and initialize some sprite graphics data: */
  78. /********************************************************/
  79. /* Sprite data for a ship: */
  80. /* (6 frames, 4 different images: 1 2 3 4 3 2) */
  81. UWORD chip ship_data[6][28]=
  82. {
  83.   {
  84.       0x0000, 0x0000, /* Ship 1 */
  85.  
  86.       0xFFF8, 0x0000,
  87.       0x0200, 0x0000,
  88.       0x877C, 0x0000,
  89.       0x8786, 0x027C,
  90.       0xBFBF, 0x02C6,
  91.       0xEDFF, 0x1AC2,
  92.       0xA57D, 0x1AFE,
  93.       0xBF19, 0x02FE,
  94.       0x8F12, 0x00FC,
  95.       0x04FC, 0x0000,
  96.       0x0809, 0x0000,
  97.       0x3FFE, 0x0000,
  98.  
  99.       0x0000, 0x0000,
  100.   },
  101.   {
  102.       0x0000, 0x0000, /* Ship 2 */
  103.  
  104.       0x7FF0, 0x0000,
  105.       0x0200, 0x0000,
  106.       0x077C, 0x0000,
  107.       0x8786, 0x027C,
  108.       0xBFBF, 0x02C6,
  109.       0xEDFF, 0x1AC2,
  110.       0xA57D, 0x1AFE,
  111.       0xBF19, 0x02FE,
  112.       0x0F12, 0x00FC,
  113.       0x04FC, 0x0000,
  114.       0x0809, 0x0000,
  115.       0x3FFE, 0x0000,
  116.  
  117.       0x0000, 0x0000,
  118.   },
  119.   {
  120.       0x0000, 0x0000, /* Ship 3 */
  121.  
  122.       0x3FE0, 0x0000,
  123.       0x0200, 0x0000,
  124.       0x877C, 0x0000,
  125.       0x8786, 0x027C,
  126.       0xBFBF, 0x02C6,
  127.       0xEDFF, 0x1AC2,
  128.       0xA57D, 0x1AFE,
  129.       0xBF19, 0x02FE,
  130.       0x8F12, 0x00FC,
  131.       0x04FC, 0x0000,
  132.       0x0809, 0x0000,
  133.       0x3FFE, 0x0000,
  134.  
  135.       0x0000, 0x0000,
  136.   },
  137.   {
  138.       0x0000, 0x0000, /* Ship 4 */
  139.  
  140.       0x1FC0, 0x0000,
  141.       0x0200, 0x0000,
  142.       0x077C, 0x0000,
  143.       0x8786, 0x027C,
  144.       0xBFBF, 0x02C6,
  145.       0xEDFF, 0x1AC2,
  146.       0xA57D, 0x1AFE,
  147.       0xBF19, 0x02FE,
  148.       0x0F12, 0x00FC,
  149.       0x04FC, 0x0000,
  150.       0x0809, 0x0000,
  151.       0x3FFE, 0x0000,
  152.  
  153.       0x0000, 0x0000,
  154.   },
  155.   {
  156.       0x0000, 0x0000, /* Ship 5 (3) */
  157.  
  158.       0x3FE0, 0x0000,
  159.       0x0200, 0x0000,
  160.       0x877C, 0x0000,
  161.       0x8786, 0x027C,
  162.       0xBFBF, 0x02C6,
  163.       0xEDFF, 0x1AC2,
  164.       0xA57D, 0x1AFE,
  165.       0xBF19, 0x02FE,
  166.       0x8F12, 0x00FC,
  167.       0x04FC, 0x0000,
  168.       0x0809, 0x0000,
  169.       0x3FFE, 0x0000,
  170.  
  171.       0x0000, 0x0000,
  172.   },
  173.   {
  174.       0x0000, 0x0000, /* Ship 6 (2) */
  175.  
  176.       0x7FF0, 0x0000,
  177.       0x0200, 0x0000,
  178.       0x077C, 0x0000,
  179.       0x8786, 0x027C,
  180.       0xBFBF, 0x02C6,
  181.       0xEDFF, 0x1AC2,
  182.       0xA57D, 0x1AFE,
  183.       0xBF19, 0x02FE,
  184.       0x0F12, 0x00FC,
  185.       0x04FC, 0x0000,
  186.       0x0809, 0x0000,
  187.       0x3FFE, 0x0000,
  188.  
  189.       0x0000, 0x0000,
  190.   }
  191. };
  192.  
  193.  
  194.  
  195. /*******************************************************/
  196. /* 2. Declare and initialize a SimpleSprite structure: */
  197. /*******************************************************/
  198. struct SimpleSprite my_sprite=
  199. {
  200.   ship_data[0],   /* posctldata, pointer to the sprite data. (Frame 0) */
  201.   12,             /* height, 12 lines tall. */
  202.   40, 80,         /* x, y, position on the screen. */
  203.   -1,             /* num, this field is automatically initialized when  */
  204.                   /* you call the GetSprite() function, so we set it to */
  205.                   /* -1 for the moment.                                 */
  206. };
  207.  
  208.  
  209.  
  210. void main()
  211. {
  212.   /* Sprite position: */
  213.   WORD x = my_sprite.x;
  214.   WORD y = my_sprite.y;
  215.  
  216.   /* Direction of the sprite: */
  217.   WORD x_direction = 0;
  218.   WORD y_direction = 0;
  219.  
  220.   UWORD frame = 0; /* Frame 0 */
  221.  
  222.   /* Boolean variable used for the while loop: */
  223.   BOOL close_me = FALSE;
  224.  
  225.   ULONG class; /* IDCMP */
  226.   USHORT code; /* Code */
  227.  
  228.   /* Declare a pointer to an IntuiMessage structure: */
  229.   struct IntuiMessage *my_message;
  230.  
  231.  
  232.  
  233.   /* Open the Intuition Library: */
  234.   IntuitionBase = (struct IntuitionBase *)
  235.     OpenLibrary( "intuition.library", 0 );
  236.   
  237.   if( IntuitionBase == NULL )
  238.     free_memory(); /* Could NOT open the Intuition Library! */
  239.  
  240.  
  241.  
  242.   /* Since we are using sprites we need to open the Graphics Library: */
  243.   /* Open the Graphics Library: */
  244.   GfxBase = (struct GfxBase *)
  245.     OpenLibrary( "graphics.library", 0);
  246.  
  247.   if( GfxBase == NULL )
  248.     free_memory(); /* Could NOT open the Graphics Library! */
  249.  
  250.  
  251.  
  252.   /* We will now try to open the window: */
  253.   my_window = (struct Window *) OpenWindow( &my_new_window );
  254.   
  255.   /* Have we opened the window succesfully? */
  256.   if(my_window == NULL)
  257.     free_memory(); /* Could NOT open the Window! */
  258.  
  259.  
  260.  
  261.   /* Change the colour register 21 - 23: */
  262.   SetRGB4( &my_window->WScreen->ViewPort, 21, 0x0, 0x0, 0x0 ); /* Black */
  263.   SetRGB4( &my_window->WScreen->ViewPort, 22, 0x0, 0x8, 0x0 ); /* DGreen */
  264.   SetRGB4( &my_window->WScreen->ViewPort, 23, 0x0, 0xD, 0x0 ); /* Green */
  265.  
  266.  
  267.  
  268.   /*******************************/
  269.   /* 3. Try to reserve sprite 2: */
  270.   /*******************************/
  271.   if( GetSprite( &my_sprite, 2 ) != 2 )
  272.     free_memory(); /* Could not reserve sprite number 2. */
  273.  
  274.  
  275.  
  276.   /* Stay in the while loop until the user has selected the Close window */
  277.   /* gadget: */
  278.   while( close_me == FALSE )
  279.   {
  280.     /* Stay in the while loop as long as we can collect messages */
  281.     /* sucessfully: */
  282.     while(my_message = (struct IntuiMessage *) GetMsg(my_window->UserPort))
  283.     {
  284.       /* After we have collected the message we can read it, and save any */
  285.       /* important values which we maybe want to check later: */
  286.       class = my_message->Class;
  287.       code  = my_message->Code;
  288.  
  289.  
  290.       /* After we have read it we reply as fast as possible: */
  291.       /* REMEMBER! Do never try to read a message after you have replied! */
  292.       /* Some other process has maybe changed it. */
  293.       ReplyMsg( my_message );
  294.  
  295.  
  296.       /* Check which IDCMP flag was sent: */
  297.       switch( class )
  298.       {
  299.         case CLOSEWINDOW:     /* Quit! */
  300.                close_me=TRUE;
  301.                break;  
  302.  
  303.         case RAWKEY:          /* A key was pressed! */
  304.                /* Check which key was pressed: */
  305.                switch( code )
  306.                {
  307.                  /* Up Arrow: */
  308.                  case 0x4C:      y_direction = -1; break; /* Pressed */
  309.                  case 0x4C+0x80: y_direction = 0;  break; /* Released */
  310.  
  311.                  /* Down Arrow: */
  312.                  case 0x4D:      y_direction = 1; break; /* Pressed */
  313.                  case 0x4D+0x80: y_direction = 0; break; /* Released */
  314.  
  315.                  /* Right Arrow: */
  316.                  case 0x4E:      x_direction = 1; break; /* Pressed */
  317.                  case 0x4E+0x80: x_direction = 0; break; /* Released */
  318.  
  319.                  /* Left Arrow: */
  320.                  case 0x4F:      x_direction = -1; break; /* Pressed */
  321.                  case 0x4F+0x80: x_direction = 0;  break; /* Released */
  322.                }
  323.                break;  
  324.       }
  325.     }
  326.  
  327.  
  328.  
  329.     /* Change the x/y position: */
  330.     x += x_direction;
  331.     y += y_direction;
  332.     
  333.     /* Check that the sprite does not move outside the screen: */
  334.     if(x > 320)
  335.       x = 320;
  336.     if(x < 0)
  337.       x = 0;
  338.     if(y > 200)
  339.       y = 200;
  340.     if(y < 0)
  341.       y = 0;
  342.  
  343.     /* Move the sprite: */
  344.     MoveSprite( 0, &my_sprite, x, y );
  345.  
  346.  
  347.  
  348.     /* Change frame: */
  349.     frame++;
  350.       
  351.     /* 6 frames: */
  352.     if( frame > 5 )
  353.       frame = 0;
  354.       
  355.     /* Change the sprite data: */
  356.     ChangeSprite( 0, &my_sprite, ship_data[ frame ] );
  357.  
  358.  
  359.  
  360.     /* Wait for the videobeam to reach the top of the display: (This */
  361.     /* will slow down the animation so the user can see the sprite)  */
  362.     /* (If you want to have some "action" you can take it away...) */
  363.     WaitTOF();
  364.   }
  365.  
  366.  
  367.  
  368.   /* Free all allocated memory: (Close the window, libraries etc) */
  369.   free_memory();
  370.  
  371.   /* THE END */
  372. }
  373.  
  374.  
  375.  
  376. /* This function frees all allocated memory. */
  377. void free_memory()
  378. {
  379.   if( my_sprite.num != -1 )
  380.     FreeSprite( my_sprite.num );
  381.  
  382.   if( my_window )
  383.     CloseWindow( my_window );
  384.   
  385.   if( GfxBase )
  386.     CloseLibrary( GfxBase );
  387.  
  388.   if( IntuitionBase )
  389.     CloseLibrary( IntuitionBase );
  390.  
  391.   exit();
  392. }
  393.